/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.helper;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.util.WebUtils;

/**
 * Miscellaneous utilities for web applications.
 */
public final class TrampolineWebUtils
{

  /**
   * private constructor.
   */
  private TrampolineWebUtils()
  {
    // Do nothing.
  }



  /**
   * Return the relative path associated with the current servlet.
   * 
   * @param request
   *          web request
   * @return the relative path associated with the current servlet
   */
  public static String getRelativePath(HttpServletRequest request)
  {
    // Are we being processed by a RequestDispatcher.include()?
    if (WebUtils.isIncludeRequest(request)) {
      String result = (String) request.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE);
      if (result == null)
        result = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE);
      if ((result == null) || (result.equals("")))
        result = "/";
      return (result);
    }

    // No, extract the desired path directly from the request
    String result = request.getPathInfo();
    if (result == null) {
      result = request.getServletPath();
    }
    if ((result == null) || (result.equals(""))) {
      result = "/";
    }
    return result;
  }
}
